import {refreshToken as refresh} from '@/api/TokenApi.js'

const USER_INFO_KEY = 'qingyun_user'

const TOKEN_KEY = 'qingyun'


export function saveUserInfo(user){

    localStorage.setItem(`${USER_INFO_KEY}`, JSON.stringify(user))

}

export function getUserInfo(){

    let user = localStorage.getItem(`${USER_INFO_KEY}`)

    return JSON.parse(user);

}


export function saveToken(token){
    localStorage.setItem(`${TOKEN_KEY}`,token);
}

export function getToken(){
    return localStorage.getItem(`${TOKEN_KEY}`);
}

export function cleareAll(){
    localStorage.removeItem(`${TOKEN_KEY}`)
    localStorage.removeItem(`${USER_INFO_KEY}`)
}

export async function refreshToken() {
    let user = getUserInfo();
    let res = await refresh(user)
    console.log(res.data,"res.data refresh");
    saveToken(res.data)

}